package com.example.easyscript.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.easyscript.common.minio.MinioUtil;
import com.example.easyscript.dto.Req.AddImageReq;
import com.example.easyscript.dto.Resp.FileUploadResp;
import com.example.easyscript.entity.task.GroupAndImage;
import com.example.easyscript.entity.task.Image;
import com.example.easyscript.entity.task.ImageGroup;
import com.example.easyscript.service.task.GroupAndImageService;
import com.example.easyscript.service.task.ImageGroupService;
import com.example.easyscript.service.task.ImageService;
import com.example.easyscript.utils.ImageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huzixian
 * @date 2023-03-08
 */
@Api(value = "ImageController", tags = "")
@Slf4j
@RestController
@RequestMapping(value = "/web/image")
public class ImageController {

    @Resource
    private MinioUtil minioUtil;

    @Resource
    private ImageService imageService;

    @Resource
    private ImageGroupService imageGroupService;

    @Resource
    private GroupAndImageService groupAndImageService;

    @PostMapping("/save")
    @ApiOperation(value = "保存图片")
    public void save(@RequestBody Image image) {
        imageService.save(image);
    }

    /**
     * 单个文件上传
     */
    @PostMapping("/uploadImage")
    @ApiOperation(value = "单个文件上传")
    public Image uploadFile(@RequestParam MultipartFile file) throws Exception {
        String path = "E:/easy_script/src/main/resources/picture/workspace/imagepool";
        Image image = new Image();
        Map<String, String> map = minioUtil.uploadFile(file);
        String savePath = ImageUtils.saveImage(file, path);
        image.setImageUrl(map.get("fileName"));
        image.setImagePath(savePath);
        return image;
    }

    /**
     * 获取文件路径
     */
    @GetMapping("/getUrl/{name}")
    @ApiOperation(value = "获取文件路径")
    public String getFileUrl(@PathVariable String name) {
        return minioUtil.fileUrl(name);
    }

    @GetMapping("/getUrlByImageId/{id}")
    @ApiOperation(value = "通过图片id获取地址")
    public String getFileUrlById(@PathVariable Integer id) {
        Image image = imageService.getById(id);
        return minioUtil.fileUrl(image.getImageUrl());
    }

    @ApiOperation("获取所有图组")
    @GetMapping("/getGroup")
    public List<ImageGroup> getGroup() {
        return imageGroupService.list();
    }

    @ApiOperation("保存图组")
    @PostMapping("/saveGroup")
    public void saveGroup(@RequestBody ImageGroup group) {
        imageGroupService.save(group);
    }

    @ApiOperation("保存组图")
    @PostMapping("/addGroupImage")
    public void addGroupImage(@RequestBody AddImageReq req) {
        groupAndImageService.addGroupImage(req);
    }

    @ApiOperation("删除组图")
    @PostMapping("/deleteImageInGroupById/{groupId}/{imageId}")
    public void deleteImageInGroupById(@PathVariable String groupId, @PathVariable String imageId) {
        QueryWrapper<GroupAndImage> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId);
        wrapper.eq("image_id", imageId);
        groupAndImageService.remove(wrapper);
    }

    @ApiOperation("通过分组获取图片")
    @GetMapping("/getImageListByGroupId/{groupId}")
    public List<Image> getImageList(@PathVariable Integer groupId) {
        if (groupId.equals(0)) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.orderByDesc("create_time");
            List<Image> imageList = imageService.list(wrapper);
            imageList.forEach(item -> {
                item.setImageUrl(minioUtil.fileUrl(item.getImageUrl()));
            });
            return imageList;
        } else {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("group_id", groupId);
            wrapper.orderByDesc("create_time");
            List<GroupAndImage> list = groupAndImageService.list(wrapper);
            List<Integer> imageIdList = list.stream().map(item -> item.getImageId()).collect(Collectors.toList());
            return imageService.getImageList(imageIdList);
        }
    }
}